package com.hy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.medicine.MedicineBasicInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface MedicineBasicInfoMapper extends BaseMapper<MedicineBasicInfo> {

    /**
     * 查询药品列表
     */
    IPage<MedicineBasicInfo> selectMedicineList(
            Page<MedicineBasicInfo> page,
            @Param("categoryId") String categoryId,
            @Param("keyword") String keyword,
            @Param("filter") String filter,
            @Param("sort") String sort,
            @Param("order") String order
    );



    // 查询药品详情(包含分类名称)
    MedicineBasicInfo selectMedicineDetailWithCategory(@Param("medicineId") Integer medicineId);

    @Update("UPDATE medicine_basic_info SET minStock = minStock - #{quantity}, " +
            "sales_volume = sales_volume + #{quantity} " +
            "WHERE medicine_id = #{medicineId} AND minStock >= #{quantity}")
    int reduceStock(@Param("medicineId") Long medicineId, @Param("quantity") Integer quantity);


    /**
     * 根据ID查询药品信息
     * @param medicineId 药品ID
     * @return 药品信息
     */
    MedicineBasicInfo selectById(Integer medicineId);


    /**
     * 更新药品库存
     * @param medicineId 药品ID
     * @param quantity 减少的数量
     * @return 影响的行数
     */
    int updateStock(@Param("medicineId") Integer medicineId, @Param("quantity") Integer quantity);

    /**
     * 检查库存是否充足
     * @param medicineId 药品ID
     * @param quantity 需要的数量
     * @return 大于0表示库存充足
     */
    int checkStock(@Param("medicineId") Integer medicineId, @Param("quantity") Integer quantity);
}
